package com.jyx.spring.factory;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.InvalidXPathException;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

import com.jyx.spring.bean.MyBeans;

public class MyClassPathXmlApplicationContext {

	private List<MyBeans> beanlist = new ArrayList<MyBeans>();
	public MyClassPathXmlApplicationContext(String filePath){
		
		try {
			SAXReader reader = new SAXReader();
			Document doc = null;
			URL xmlPath = this.getClass().getClassLoader().getResource(filePath);
			
			Map<String,String> nsMap = new HashMap<String,String>();
			nsMap.put("ns", "http://www.springframework.org/schema/beans");
			doc = reader.read(xmlPath);
			XPath xpath = doc.createXPath("//ns:beans//ns:bean");// 创建//ns:beans//ns:bean查询路径
			xpath.setNamespaceURIs(nsMap);
			List<Element> eles = xpath.selectNodes(doc);// 取得文档下所有节点
			for (Element element : eles) {
				String id = element.attributeValue("id");
				String cn = element.attributeValue("class");
				//自定义实体bean，保存配置文件中id和class
				MyBeans beans = new MyBeans(id, cn);
				beanlist.add(beans);
			 }
		} catch (InvalidXPathException | DocumentException e) {
			e.printStackTrace();
		}
	}
	
	private void initBeans(){
		
		for (MyBeans bean : beanlist) {
			if(bean.getClass().getName() == null || "".equals(bean.getClass().getName())){
				
			}
		}
	}
	
}
